#include "mySql.h"

mySql::mySql(QWidget *parent)
    : QWidget(parent)
{
    ui.setupUi(this);
	this->setWindowTitle("MYSQL数据库连接");
	this->setWindowIcon(QIcon(":/images/images/Qicon.jpg"));
	this->database = QSqlDatabase::addDatabase("QMYSQL");
	this->database.setPort(3306);
	this->database.setHostName("localhost");
	this->database.setDatabaseName("mydatabase");
	this->database.setUserName("root");
	this->database.setPassword("@Whj521520");
	if (this->database.open())
	{
		QMessageBox::information(this, "连接数据库提示", "连接数据库成功!");	
	}
	else
	{
		QMessageBox::warning(this, "连接数据库提示", "连接数据库失败");
	}
}

mySql::~mySql()
{}

void mySql::on_insertPushButton_clicked()
{
	//qDebug() << QSqlDatabase::drivers();
	QString strId = ui.idLineEdit->text();
	QString strName = ui.nameLineEdit->text();
	QString strBirth = ui.birthLineEdit->text();
	QString strSQL = QString("insert into student values (%1, '%2', '%3');").arg(strId).arg(strName).arg(strBirth);
	QSqlQuery query;
	if (query.exec(strSQL))
	{
		QMessageBox::information(this, "插入提示", "插入数据成功!");
	}
	else
	{
		QMessageBox::warning(this, "插入提示", "插入数据失败!");
	}
}

void mySql::on_foundPushButton_clicked()
{
	QSqlQuery query;
	query.exec("select * from student;");
	while (query.next())
	{
		qDebug() << query.value(0);
		qDebug() << query.value(1);
		qDebug() << query.value(2);
	}
}

void mySql::on_tablePushButton_clicked()
{
	//this->hide();
	table *t = new table();
	t->show();
}
